# -*- coding: utf-8 -*-            
# @Time : 2025/6/27 10:09
# @FileName: request_maixun_modelId.py
# @Target:
'''
保存这里的Template，用于直接从脉讯中获取原始数据信息
'''
import json, codecs, time, os, requests, xlwt
from pprint import pprint
from tqdm import tqdm

cache = {}


class MAIXUN:
    def __init__(self):
        self.headers = {
            'User-Agent': 'Apipost client Runtime/+https://www.apipost.cn/',
            'Authorization': 'Bearer 63ae2f983c82264a48f17ee3c8250fcc',
        }

    def request_all_data(self, fid, size=100, offset=1):
        headers = self.headers
        data = {
            'size': size,
            'fid': fid,
            'offset': offset,
            'need_full_text': 0
        }
        while True:
            data.update(offset=offset)
            response = requests.post(
                'http://api.cem.maixunbytes.com/cyberin/xpost/posts/list-by-offset',
                headers=headers,
                data=data
            ).json()
            print(response)
            # Store parsing result response ['data ']
            # Store offset
            if response and response.get('max_offset'):
                offset = response['max_offset'] + 1
            time.sleep(5)


    def request_modelId(self):
        headers = self.headers
        response = requests.post(
            "http://api.cem.maixunbytes.com/cyberin/xpost/other/obj_info",
            headers=headers
        ).json()
        data = response['data']
        all_data = []
        for d in data:
            fid = d['fid']
            name = d['name']
            oid = d['oid']
            synonyms = d['synonyms']
            all_data.append({
                'fid': fid,
                'name': name,
                'oid': oid,
                'synonyms': synonyms,
            })
        return  all_data



maixun = MAIXUN()

if __name__ == '__main__':
    all_data = maixun.request_modelId()
    with codecs.open(filename='maixun_modelId_name_map.json', mode='w',
                     encoding='utf-8') as fw:
        json.dump(obj=all_data, fp=fw, ensure_ascii=False, indent=4)

    # a = all_data
    # title = list(set([j for i in a for j in i]))
    # book = xlwt.Workbook()
    # sheet = book.add_sheet('Sheet1', cell_overwrite_ok=True)  # 添加一个sheet页
    # for i in range(len(title)):  # 循环列
    #     sheet.write(0, i, title[i])  # 将title数组中的字段写入到0行i列中
    # for i, it in enumerate(a):
    #     for j, k in enumerate(title):
    #         sheet.write(1 + i, j, it[k])
    #
    # book.save('haha.xls')





